/*
 * Copyright (c) 2001, Swedish Institute of Computer Science.
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 *
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 *
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *
 * 3. Neither the name of the Institute nor the names of its contributors
 *    may be used to endorse or promote products derived from this software
 *    without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGE.
 *
 * Author: Adam Dunkels <adam@sics.se>
 *
 * $Id: tapdev.c,v 1.8 2006/06/07 08:39:58 adam Exp $
 */

#define UIP_DRIPADDR0   192
#define UIP_DRIPADDR1   168
#define UIP_DRIPADDR2   0
#define UIP_DRIPADDR3   1

#include "CoOS.h"
#include "shell.h"

#include "uip.h"
#include "uip_arp.h"

#define TAP_MSG(...)	DBG_MSG(3,__VA_ARGS__)

/*---------------------------------------------------------------------------*/
void
tapdev_init(void)
{
	struct uip_eth_addr macaddr = {{0x00,0xFF,0x6F,0x95,0x0E,0xEF}};
	ENC28J60_Init(macaddr.addr);
	uip_setethaddr(macaddr);
}
/*---------------------------------------------------------------------------*/
unsigned int
tapdev_read(void)
{
	unsigned int len = ENC28J60_PacketReceive(UIP_BUFSIZE, uip_buf);
	
	if(len > 0)
	{
		U32 u32i;
		TAP_MSG("Rev Packet %d\n", len);
		for(u32i = 0; u32i<len; u32i++)
		{
			if(u32i % 16 == 0)
			{
				TAP_MSG("\n");
			}

			TAP_MSG("%02x ", uip_buf[u32i]);
		}
		TAP_MSG("\n");
	}
	
	return len;
}
/*---------------------------------------------------------------------------*/
void
tapdev_send(void)
{
	
	if(uip_len > 0)
	{
		U32 u32i;
		TAP_MSG("Send Packet %d\n", uip_len);
		for(u32i = 0; u32i<uip_len; u32i++)
		{
			if(u32i % 16 == 0)
			{
				TAP_MSG("\n");
			}

			TAP_MSG("%02x ", uip_buf[u32i]);
		}
		TAP_MSG("\n");
	}
	
	ENC28J60_PacketSend(uip_len, uip_buf);
}
/*---------------------------------------------------------------------------*/
